package gui.customizer.panes.netfilter;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import core.models.NatRule;

public class ConnectionSharing extends NatConfigurable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6928162465811305343L;
	private JTextField txtSource;
	private JTextField txtDestination;
	private JTextField txtWanIp;
	private JCheckBox chckbxDestInv;
	private JComboBox cmbWanInterface;
	private JCheckBox chckbxMasquerade;
	private JCheckBox chckbxSrcInv;

	/**
	 * Create the panel.
	 */
	public ConnectionSharing() {
		setLayout(new MigLayout("", "[grow][]20[grow][]", "[][]15[][]15[][][]15[]"));
		
		JLabel lblSource = new JLabel("Source");
		add(lblSource, "cell 0 0");
		
		JLabel lblDestination = new JLabel("Destination");
		add(lblDestination, "cell 2 0");
		
		txtSource = new JTextField();
		add(txtSource, "cell 0 1,growx");
		txtSource.setColumns(10);
		
		chckbxSrcInv = new JCheckBox("!");
		add(chckbxSrcInv, "cell 1 1");
		
		txtDestination = new JTextField();
		add(txtDestination, "cell 2 1,growx");
		txtDestination.setColumns(10);
		
		chckbxDestInv = new JCheckBox("!");
		add(chckbxDestInv, "cell 3 1");
		
		JLabel lblWanInterface = new JLabel("WAN Interface");
		add(lblWanInterface, "cell 0 2");
		
		cmbWanInterface = new JComboBox();
		add(cmbWanInterface, "cell 0 3,growx");
		try {
			cmbWanInterface.setModel(getAvailableInterfaces());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		JLabel lblWanIp = new JLabel("WAN IP");
		add(lblWanIp, "cell 0 4");
		
		txtWanIp = new JTextField();
		add(txtWanIp, "cell 0 5,growx");
		txtWanIp.setColumns(10);
		
		chckbxMasquerade = new JCheckBox("Masquerade");
		chckbxMasquerade.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				if (chckbxMasquerade.isSelected()) {
					txtWanIp.setEnabled(false);
				} else {
					txtWanIp.setEnabled(true);
				}
			}
		});
		add(chckbxMasquerade, "cell 0 6");
		
		btnAdd = new JButton("Add");
		add(btnAdd, "cell 0 7");
	}
	
	@Override
	public void setRule(NatRule rule) {
		HashMap<String, String> options = rule.getOptions();
		
		// Source
		if (options.containsKey("-s")) {
			txtSource.setText(options.get("-s"));
			chckbxSrcInv.setSelected(false);
		} else if (options.containsKey("! -s")) {
			txtSource.setText(options.get("! -s"));
			chckbxSrcInv.setSelected(true);
		} else {
			txtSource.setText("");
			chckbxSrcInv.setSelected(false);
		}
		
		// Destination
		if (options.containsKey("-d")) {
			txtDestination.setText(options.get("-d"));
			chckbxDestInv.setSelected(false);
		} else if (options.containsKey("! -d")) {
			txtDestination.setText(options.get("! -d"));
			chckbxDestInv.setSelected(true);
		} else {
			txtDestination.setText("");
			chckbxDestInv.setSelected(true);
		}
		
		// WAN Interface
		if (options.containsKey("-o")) {
			cmbWanInterface.setSelectedItem(options.get("-o"));
		} else {
			cmbWanInterface.setSelectedItem("-none-");
		}
		
		// WAN IP
		String[] target = options.get("-j").split(" ");
		if (target.length == 3 && target[0].equals("SNAT") && target[1].matches("--to-(source)?")) {
			chckbxMasquerade.setSelected(false);
			txtWanIp.setText(target[2]);
		} else if (target.length == 1 && target[0].equals("MASQUERADE")) {
			chckbxMasquerade.setSelected(true);
			txtWanIp.setText("");
		} else {
			chckbxMasquerade.setSelected(false);
			txtWanIp.setText("");
		}
		
	}

	@Override
	public NatRule getRule() {
		NatRule rule = new NatRule("-A", "POSTROUTING");
		
		// Source
		if (!txtSource.getText().equals("")) {
			if (chckbxSrcInv.isSelected())
				rule.addOption("! -s", txtSource.getText());
			else
				rule.addOption("-s", txtSource.getText());
		}
		
		// Destination
		if (!txtDestination.getText().equals("")) {
			if (chckbxDestInv.isSelected())
				rule.addOption("! -d", txtDestination.getText());
			else
				rule.addOption("-d", txtDestination.getText());
		}
		
		// Wan Interface
		if (!cmbWanInterface.getSelectedItem().equals("-none-")) {
				rule.addOption("-o", (String) cmbWanInterface.getSelectedItem());
		}
		
		// WAN IP
		if (chckbxMasquerade.isSelected()) {
			rule.addOption("-j", "MASQUERADE");
		} else {
			rule.addOption("-j", "SNAT --to-source " + txtWanIp.getText());
		}
		
		return rule;
	}

	@Override
	public void resetFields() {
		txtSource.setText("");
		chckbxSrcInv.setSelected(false);
		txtDestination.setText("");
		chckbxDestInv.setSelected(false);
		cmbWanInterface.setSelectedItem("-none");
		txtWanIp.setText("");
		chckbxMasquerade.setSelected(false);
	}
}
